package com.apotik.legenda.main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.KeyStroke;

import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;

import javax.swing.JSeparator;
import javax.swing.ImageIcon;

import com.apotik.legenda.frameinternal.BarangInternalFrame;
import com.apotik.legenda.frameinternal.CardInternalFrame;
import com.apotik.legenda.frameinternal.DokterInternalFrame;
import com.apotik.legenda.frameinternal.InsuranceInternalFrame;
import com.apotik.legenda.frameinternal.LoginInternalFrame;
import com.apotik.legenda.frameinternal.PembelianInternalFrame;
import com.apotik.legenda.frameinternal.SelectKasirInternalFrame;
import com.apotik.legenda.frameinternal.SupplierInternalFrame;
import com.apotik.legenda.frameinternal.UserInternalFrame;
import com.apotik.legenda.model.User;
import com.apotik.legenda.observer.IndexObserver;
import com.apotik.legenda.util.AppUtil;
import com.apotik.legenda.util.HibernateUtil;
import com.apotik.legenda.util.PrinterUtil;

import javax.swing.border.MatteBorder;
import javax.swing.border.EmptyBorder;

public class Index implements IndexObserver, ActionListener {
	
	private JFrame frame;
	private JDesktopPane desktopPane;
	
	private JMenu mnFile;
	private JMenu mnKasir;
	private JMenu mnUser;
	private JMenu mnData;
	private JMenu mnLaporan;
	private JMenu mnSetting;
	private JMenu mnHelp; 
	
	private JMenuItem mntmPointOfSales;
	private JMenuItem mntmBarang; 
	private JMenuItem mntmUpdateAkun;
	private JMenuItem mntmManajemenUser;
	private JMenuItem mntmTransPembelian;
	private JMenuItem mntmDoctors;
	private JMenuItem mntmSupplier;
	private JMenuItem mntmCustomers;
	private JMenuItem mntmLogOut;
	private JMenuItem mntmShift;
	private JMenuItem mntmPrinterDefault;
	
	private BarangInternalFrame barangInternalFrame;
	private UserInternalFrame userInternalFrame;
	private SupplierInternalFrame supplierInternalFrame;
	private DokterInternalFrame dokterInternalFrame;
	private PembelianInternalFrame pembelianInternalFrame;
	private InsuranceInternalFrame insuransiInternalFrame;
	private CardInternalFrame cardInternalFrame;
	
	private JSeparator separator;
	private User user;
	private JPanel panel_1;
	private JMenuItem mntmCard;
	private JMenuItem mntmAsuransi;
	
	public static void main(String[] args) {
		try {
			HibernateUtil.getSessionFactory().openSession();
			HibernateUtil.shutdown();
			
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					try {
						Index window = new Index();
						window.frame.setVisible(true);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
	}

	
	public Index() {
		initialize();
	}
	
	private void initialize() {
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		
		frame = new JFrame();
		frame.setTitle("Apotik");
		frame.setBounds(50, 50, ((int) dim.getWidth())-100, ((int) dim.getHeight())-100);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
		
		JMenuBar menuBar = menuBar();
		frame.setJMenuBar(menuBar);
		
		JDesktopPane desktopPane = desktopPane();
		frame.getContentPane().add(desktopPane, BorderLayout.CENTER);
		
		JPanel panel = panel();
		frame.getContentPane().add(panel, BorderLayout.SOUTH);
		
		firstCondition();
	}
	
	
	
	private JMenuBar menuBar(){
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		//Menu File
		mnFile = new JMenu("File");
		mnFile.setSelectedIcon(new ImageIcon(Index.class.getResource("/images/pos.jpg")));
		mnFile.setMnemonic(KeyEvent.VK_F);
		menuBar.add(mnFile);
		
		separator = new JSeparator();
		mnFile.add(separator);
		
		mntmLogOut = new JMenuItem("LogOut");
		mntmLogOut.setIcon(AppUtil.resizeImage(Index.class, "/images/logout.png", 16, 16));
		mntmLogOut.addActionListener(this);
		mnFile.add(mntmLogOut);
		
		separator = new JSeparator();
		mnFile.add(separator);
		
		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.setIcon(new ImageIcon(Index.class.getResource("/javax/swing/plaf/metal/icons/ocean/close.gif")));
		mnFile.add(mntmExit);
		
		//Menu Transaction
		/*mnTransaction = new JMenu("Transaksi");
		menuBar.add(mnTransaction);
		
		mntmTransPenjualan = new JMenuItem("Penjualan Barang");
		mntmTransPenjualan.setIcon(AppUtil.resizeImage(Index.class, "/images/sale.png", 16, 16));
		mntmTransPenjualan.addActionListener(this);
		mnTransaction.add(mntmTransPenjualan);*/
		
		//Menu Kasir
		mnKasir = new JMenu("Kasir");
		mnKasir.setMnemonic(KeyEvent.VK_K);
		menuBar.add(mnKasir);
		
		mntmPointOfSales = new JMenuItem("Point of Sales", AppUtil.resizeImage(Index.class, "/images/pos.jpg", 16, 16));
		mntmPointOfSales.addActionListener(this);
		mntmPointOfSales.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_K, InputEvent.CTRL_MASK));
		mnKasir.add(mntmPointOfSales);
		
		//Menu User
		mnUser = new JMenu("User");
		mnUser.setMnemonic(KeyEvent.VK_U);
		menuBar.add(mnUser);
		
		mntmUpdateAkun = new JMenuItem("Update Akun");
		mnUser.add(mntmUpdateAkun);
		
		separator = new JSeparator();
		mnUser.add(separator);
		
		mntmManajemenUser = new JMenuItem("Manajemen Users", AppUtil.resizeImage(Index.class, "/images/list.png", 16, 16));
		mntmManajemenUser.addActionListener(this);
		mnUser.add(mntmManajemenUser);
		
		//Menu Data
		mnData = new JMenu("Data");
		mnData.setMnemonic(KeyEvent.VK_D);
		menuBar.add(mnData);
		
		mntmTransPembelian = new JMenuItem("Pembelian Barang");
		mntmTransPembelian.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_MASK));
		mntmTransPembelian.addActionListener(this);
		mntmTransPembelian.setIcon(AppUtil.resizeImage(Index.class, "/images/purchase.png", 16, 16));
		mnData.add(mntmTransPembelian);
		
		mntmBarang = new JMenuItem("Barang");
		mntmBarang.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, InputEvent.CTRL_MASK));
		mntmBarang.addActionListener(this);
		mntmBarang.setIcon(AppUtil.resizeImage(Index.class, "/images/item.png", 16, 16));
		mnData.add(mntmBarang);
		
		separator = new JSeparator();
		mnData.add(separator);
		
		mntmSupplier = new JMenuItem("Supplier");
		mntmSupplier.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
		mntmSupplier.addActionListener(this);
		mntmSupplier.setIcon(AppUtil.resizeImage(Index.class, "/images/suppliers.png", 16, 16));
		mnData.add(mntmSupplier);
		
		separator = new JSeparator();
		mnData.add(separator);
		
		mntmDoctors = new JMenuItem("Dokter");
		mntmDoctors.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_MASK));
		mntmDoctors.addActionListener(this);
		mntmDoctors.setIcon(AppUtil.resizeImage(Index.class, "/images/doctor.png", 16, 16));
		mnData.add(mntmDoctors);
		
		mntmCustomers = new JMenuItem("Pelanggan");
		mntmCustomers.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));
		mntmCustomers.addActionListener(this);
		mntmCustomers.setIcon(AppUtil.resizeImage(Index.class, "/images/customer.png", 16, 16));
		mnData.add(mntmCustomers);
		
		separator = new JSeparator();
		mnData.add(separator);
		
		mntmAsuransi = new JMenuItem("Asuransi");
		mntmAsuransi.addActionListener(this);
		mnData.add(mntmAsuransi);
		
		mntmCard = new JMenuItem("Kartu Debit");
		mntmCard.addActionListener(this);
		mnData.add(mntmCard);
		
		//Menu laporan
		mnLaporan = new JMenu("Laporan");
		mnLaporan.setMnemonic(KeyEvent.VK_L);
		menuBar.add(mnLaporan);
		
		//Menu Setting
		mnSetting = new JMenu("Setting");
		mnSetting.setMnemonic(KeyEvent.VK_S);
		menuBar.add(mnSetting);
		
		mntmShift = new JMenuItem("Shift Kasir");
		mntmShift.addActionListener(this);
		mnSetting.add(mntmShift);
		
		mntmPrinterDefault = new JMenuItem("Info Default Printer");
		mntmPrinterDefault.addActionListener(this);
		mnSetting.add(mntmPrinterDefault);
		
		//Menu Help
		mnHelp = new JMenu("Help");
		mnHelp.setMnemonic(KeyEvent.VK_H);
		menuBar.add(mnHelp);
		
		JMenuItem mntmAbout = new JMenuItem("About");
		mntmAbout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				/* Sample sukses custom dialog bentuk form
				JTextField firstName = new JTextField();
				JTextField lastName = new JTextField();
				JPasswordField password = new JPasswordField();
				final JComponent[] inputs = new JComponent[] {
						new JLabel("First"),
						firstName,
						new JLabel("Last"),
						lastName,
						new JLabel("Password"),
						password
				};
				JOptionPane.showMessageDialog(null, inputs, "My custom dialog", JOptionPane.PLAIN_MESSAGE);
				System.out.println("You entered " +
						firstName.getText() + ", " +
						lastName.getText() + ", " +
						password.getText());*/
			}
		});
		mnHelp.add(mntmAbout);
		
		return menuBar;
	}
	
	private JDesktopPane desktopPane(){
		desktopPane = new JDesktopPane();
		desktopPane.setBackground(Color.gray);
		
		showLoginInternalFrame();
		
		return desktopPane;
	}
	
	private JPanel panel(){
		JPanel panel = new JPanel();
		panel.setBorder(new MatteBorder(1, 0, 0, 0, (Color) Color.BLACK));
		panel.setLayout(new BorderLayout());
		
		panel_1 = new JPanel();
		panel_1.setBorder(new EmptyBorder(3, 2, 3, 2));
		panel.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JLabel lblNewLabel = new JLabel("Copyright(c)");
		panel_1.add(lblNewLabel, BorderLayout.WEST);
		
		return panel;
	}
	
	private void firstCondition(){
		mnFile.setVisible(false);
		//mnTransaction.setVisible(false);
		mnKasir.setVisible(false);
		mnUser.setVisible(false);
		mnData.setVisible(false);
		mnLaporan.setVisible(false);
		mnSetting.setVisible(false);
		
		this.user = null;
	}
	
	private void showLoginInternalFrame(){
		try {
			LoginInternalFrame loginIf = new LoginInternalFrame(Index.this);
			desktopPane.add(loginIf);
			loginIf.show();
			loginIf.setSelected(true);
			loginIf.requestFocus();	
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		firstCondition();
	}
	
	
	public JFrame getFrame() {
		return frame;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		//System.out.println("msukk: "+obj);
		
		try {
			if(obj==mntmBarang){
				boolean isVisible = false;
				if(barangInternalFrame!=null){
					isVisible = barangInternalFrame.isVisible();
				}
				
				if(isVisible){
					barangInternalFrame.toFront();
					barangInternalFrame.setSelected(true);
					barangInternalFrame.requestFocus();
				}else{
					barangInternalFrame = new BarangInternalFrame(user);
					desktopPane.add(barangInternalFrame);
					barangInternalFrame.show();
					barangInternalFrame.setSelected(true);
					barangInternalFrame.requestFocus();
					barangInternalFrame.setMaximum(true);
				}
			}else if(obj==mntmManajemenUser){
				boolean isVisible = false;
				if(userInternalFrame!=null){
					isVisible = userInternalFrame.isVisible();
				}
				
				if(isVisible){
					userInternalFrame.toFront();
					userInternalFrame.setSelected(true);
					userInternalFrame.requestFocus();
				}else{
					userInternalFrame = new UserInternalFrame(user);
					desktopPane.add(userInternalFrame);
					userInternalFrame.show();
					userInternalFrame.setSelected(true);
					userInternalFrame.requestFocus();
					//userInternalFrame.setMaximum(true);
				}
			}else if(obj==mntmSupplier){
				boolean isVisible = false;
				if(supplierInternalFrame!=null){
					isVisible = supplierInternalFrame.isVisible();
				}
				
				if(isVisible){
					supplierInternalFrame.toFront();
					supplierInternalFrame.setSelected(true);
					supplierInternalFrame.requestFocus();
				}else{
					supplierInternalFrame = new SupplierInternalFrame(user);
					desktopPane.add(supplierInternalFrame);
					supplierInternalFrame.show();
					supplierInternalFrame.setSelected(true);
					supplierInternalFrame.requestFocus();
					supplierInternalFrame.setMaximum(true);
				}
			}else if(obj==mntmDoctors){
				boolean isVisible = false;
				if(dokterInternalFrame!=null){
					isVisible = dokterInternalFrame.isVisible();
				}
				
				if(isVisible){
					dokterInternalFrame.toFront();
					dokterInternalFrame.setSelected(true);
					dokterInternalFrame.requestFocus();
				}else{
					dokterInternalFrame = new DokterInternalFrame(user);
					desktopPane.add(dokterInternalFrame);
					dokterInternalFrame.show();
					dokterInternalFrame.setSelected(true);
					dokterInternalFrame.requestFocus();
					dokterInternalFrame.setMaximum(true);
				}
			}else if(obj==mntmPointOfSales){
				SelectKasirInternalFrame internalFrame = new SelectKasirInternalFrame(getFrame());
				desktopPane.add(internalFrame);
				internalFrame.show();
				internalFrame.setSelected(true);
				internalFrame.requestFocus();
			}else if(obj==mntmLogOut){
				JInternalFrame[] internalFrameArr = desktopPane.getAllFrames();
				for(int i=0;i<internalFrameArr.length;i++){
					JInternalFrame internalFrame = internalFrameArr[i];
					internalFrame.setVisible(false);
				}
				
				showLoginInternalFrame();
			}else if(obj==mntmTransPembelian){
				boolean isVisible = false;
				if(pembelianInternalFrame!=null){
					isVisible = pembelianInternalFrame.isVisible();
				}
				
				if(isVisible){
					pembelianInternalFrame.toFront();
					pembelianInternalFrame.setSelected(true);
					pembelianInternalFrame.requestFocus();
				}else{
					pembelianInternalFrame = new PembelianInternalFrame(user);
					desktopPane.add(pembelianInternalFrame);
					pembelianInternalFrame.show();
					pembelianInternalFrame.setSelected(true);
					pembelianInternalFrame.requestFocus();
					pembelianInternalFrame.setMaximum(true);
				}
			}else if(obj==mntmAsuransi){
				boolean isVisible = false;
				if(insuransiInternalFrame!=null){
					isVisible = insuransiInternalFrame.isVisible();
				}
				
				if(isVisible){
					insuransiInternalFrame.toFront();
					insuransiInternalFrame.setSelected(true);
					insuransiInternalFrame.requestFocus();
				}else{
					insuransiInternalFrame = new InsuranceInternalFrame(user);
					desktopPane.add(insuransiInternalFrame);
					insuransiInternalFrame.show();
					insuransiInternalFrame.setSelected(true);
					insuransiInternalFrame.requestFocus();
					//insuransiInternalFrame.setMaximum(true);
				}
			}else if(obj==mntmCard){
				boolean isVisible = false;
				if(cardInternalFrame!=null){
					isVisible = cardInternalFrame.isVisible();
				}
				
				if(isVisible){
					cardInternalFrame.toFront();
					cardInternalFrame.setSelected(true);
					cardInternalFrame.requestFocus();
				}else{
					cardInternalFrame = new CardInternalFrame(user);
					desktopPane.add(cardInternalFrame);
					cardInternalFrame.show();
					cardInternalFrame.setSelected(true);
					cardInternalFrame.requestFocus();
					//insuransiInternalFrame.setMaximum(true);
				}
			}else if(obj==mntmPrinterDefault){
				PrinterUtil.infoPrinterAll();
			}
			
		} catch (Exception e2) {
			JOptionPane.showMessageDialog(null, e2.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
	}
	
	@Override
	public void loginResult(boolean isSuccess, User user) {
		if(isSuccess){
			Index.this.user = user;
			
			mnFile.setVisible(true);
			
			if(user.getRole().getRoleCode().equalsIgnoreCase(AppUtil.ROLE_SUPER_ADMIN)){
				//mnTransaction.setVisible(true);
				mnKasir.setVisible(true);
				mnUser.setVisible(true);
				mnData.setVisible(true);
				mnLaporan.setVisible(true);
				mnSetting.setVisible(true);
				//mntmPointOfSales.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_K, InputEvent.CTRL_MASK));
				
				mntmManajemenUser.setVisible(true);

				
			}else if(user.getRole().getRoleCode().equalsIgnoreCase(AppUtil.ROLE_OWNER)){
				//mnTransaction.setVisible(true);
				mnKasir.setVisible(true);
				mnUser.setVisible(true);
				mnData.setVisible(true);
				mnLaporan.setVisible(true);
				mnSetting.setVisible(true);
				
				mntmManajemenUser.setVisible(true);
				
			}else if(user.getRole().getRoleCode().equalsIgnoreCase(AppUtil.ROLE_ADMIN)){
				//mnTransaction.setVisible(false);
				mnKasir.setVisible(false);
				mnUser.setVisible(true);
				mnData.setVisible(true);
				mnLaporan.setVisible(false);
				mnSetting.setVisible(false);
				
				mntmManajemenUser.setVisible(true);
				
			}else if(user.getRole().getRoleCode().equalsIgnoreCase(AppUtil.ROLE_KASIR)){
				//mnTransaction.setVisible(false);
				mnKasir.setVisible(false);
				mnUser.setVisible(true);
				mnData.setVisible(false);
				mnLaporan.setVisible(false);
				mnSetting.setVisible(false);
				
				mntmManajemenUser.setVisible(false);
				
			}else if(user.getRole().getRoleCode().equalsIgnoreCase(AppUtil.ROLE_STAFF)){
				//
			}
		}
	}
}
